package com.imooc.sellspringcloud.server.redis;

import com.imooc.sellspringcloud.server.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author
 * @create 2019-08-07 20:14
 **/
@Service
public class RedisService {

    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 设置缓冲
     *
     * @param keyPrefix 前缀
     * @param key
     * @param val
     * @param <T>
     * @return
     */
    public <T> boolean set(KeyPrefix keyPrefix, String key, T val) {
        try {
            String newVal = beanToString(val);
            if (newVal == null || newVal.length() <= 0)
                return false;
            String k = keyPrefix.getPrefix() + key;
            redisTemplate.opsForValue().set(k, newVal);
            if (keyPrefix.expireSeconds() > 0) {
                redisTemplate.expire(k, keyPrefix.expireSeconds(), TimeUnit.MINUTES);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public <T> T get(KeyPrefix keyPrefix, String key, Class<T> clazz) {
        try {
            String str = (String) redisTemplate.opsForValue().get(keyPrefix.getPrefix() + key);
            return (T) stringToBean(str, clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public Long desc(KeyPrefix keyPrefix, String key, Long number) {
        try {
            if (number != null && number > 0) {
                return redisTemplate.opsForValue().decrement(keyPrefix.getPrefix() + key, number);
            } else {
                return redisTemplate.opsForValue().decrement(keyPrefix.getPrefix() + key);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String beanToString(Object obj) {
        if (Objects.isNull(obj)) {
            return null;
        }
        Class<?> aClass = obj.getClass();
        if (aClass == int.class || aClass == Integer.class) {
            return "" + obj;
        } else if (aClass == long.class || aClass == Long.class) {
            return "" + obj;
        } else if (aClass == String.class) {
            return (String) obj;
        } else {
            return JsonUtil.toJson(obj);
        }
    }

    public static <T> Object stringToBean(String json, Class<T> clazz) {
        if (Objects.isNull(json)) {
            return null;
        }

        if (clazz == int.class || clazz == Integer.class) {
            return Integer.valueOf(json);
        } else if (clazz == long.class || clazz == Long.class) {
            return Long.valueOf(json);
        } else if (clazz == String.class) {
            return json;
        } else {
            return JsonUtil.fromJson(json, clazz);
        }
    }

}